/* File: ExposedAttribute.java
 * Purpose: Represents an exposed attribute for an organism.
 * Author: Team Karp
 * Date: 12/13/07
 * Revisions: 12/13/07 - Adam Patterson - Initial Version
 *            [2007-12-20, Ryan J. Bovorasmy] Implemented. TODO: Implement 
              generateID() if necessary.
 *            [2008-01-13 Adam Patterson] Implemented compareTo
 * Additional Requirements:
 */
package protovitae.data;

import org.w3c.dom.Document;
import org.w3c.dom.Element;



/**
 * Represents an exposed attribute for an organism.
 * @version 1.0
 * @created 06-Dec-2007 16:19:09
 */
public class ExposedAttribute implements XMLSavable {
    private int current;
    private int id;
    private int max;
    //private SaveStrategy saveStrategy;
    
    public ExposedAttribute(){
        generateId();
    }
    
    /**
     * @param id The id of this ExposedAttribute.
     * @param max The initial max value.
     * @param full If true, initialize at max, if false = initialize at 0.
     */
    public ExposedAttribute(int id, int max, boolean full){
        this.id = id;
        this.setMax(max);
        this.setCurrent((full)? max: 0);
    }
    
    /**
     * Dummy method for creating a valid id. This may not be used:
     * An Organism can set the ID, because it knows which attribute is
     * being used.
     */
    private void generateId() {
        this.setId(0);
    }
    
    /**
     * Save chain for attributes.
     * @param parent
     */
    public void save(Element parentElm, Document doc){
        Element root = doc.createElement("atr");
        
        Element idelm = doc.createElement("attr_id");
        Element curelm = doc.createElement("current");
        Element maxelm = doc.createElement("max");
        
        idelm.appendChild(doc.createTextNode(""+id));
        root.appendChild(idelm);
        curelm.appendChild(doc.createTextNode(""+current));
        root.appendChild(curelm);
        maxelm.appendChild(doc.createTextNode(""+max));
        root.appendChild(maxelm);
        
        parentElm.appendChild(root);
    }

    public void load(Element input) {
        try{
            id = Integer.parseInt(input.getElementsByTagName("attr_id").item(0).getTextContent());
            current = Integer.parseInt(input.getElementsByTagName("current").item(0).getTextContent());
            max = Integer.parseInt(input.getElementsByTagName("max").item(0).getTextContent());
        }
        catch(Exception e){
            
        }
    }
    
    
    
    /**
     * Returns the maximum value of the attribute.
     */
    public int getMax(){
        return max;
    }
    
    /**
     * Sets the current value.
     * @param current The current value of this ExposedAttribute.
     */
    public void setCurrent(int current){
        if(current>this.max){
            current = this.max;
        }
        if(current<0){
            current = 0;
        }
        this.current = current;
    }
    
    /**
     * Sets the max value.
     * @param max The max value of this ExposedAttribute.
     */
    public void setMax(int max){
        this.max = max;
    }
    
    /**
     * Gets the current value.
     * @return The current value of this exposed attribute.
     */
    public int getCurrent() {
        return current;
    }
    
    /**
     * Returns the ID.
     * @return the id of this ExposedAttribute.
     */
    public int getId() {
        return id;
    }
    
    /**
     * Sets the ID.
     * @param id The id to set.
     */
    public void setId(int id) {
        this.id = id;
    }
    
    /**
     * Compare two attributes
     * @param compareTo - the ExposedAttribute being compared to
     * @return >0 if this > compareTo; 0 if values are equal and <0 in all
     *      other cases
     */
    public int compare(ExposedAttribute compareTo){
        return this.current-compareTo.current;
    }
    
}